/**
 * 法人组织参数设置
 * @author liuw
 * @date 2019/09/30
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_param = {},
        cols = [[
            {type: 'checkbox', fixed: 'left'},
            {
                field: 'GWAZLEGAL_NAME',
                width: 160,
                title: '法人说明',
                fixed: 'left',
                sort: true
            }
            , {field: 'GWAZLEGAL', width: 160, title: '法人编号',hide:true, search: 'default', searchOpts: {icon: true}}
            , {field: 'GWAZ001', width: 160, title: '参数类型', search: 'default'}
            , {field: 'GWAZ002', width: 160, title: '参数值', search: 'default'}
            , {field: 'GWAZ003', width: 160, title: '备注'}
            , {field: 'GWAZACTI', width: 160, title: '有效', filterDatas: app.datas_('sys_y_n')}
            ,{
                width: 60,
                title: '',
                toolbar: '#script-gwas100setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwas100SettingAction,
        editArea = ['600px', '400px']
    ;

    var Gwas100 = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwas100',
                _searchs: [{
                    id: 'gwas100', tableId: 'gwas100_table', datas: cols[0]
                }],
                _loadTplsFull: [
                    {
                        dom: 'gwas100_edit', tpl: 'gwas100_edit'
                    },
                    {
                        dom: 'common_open_window', tpl: 'common/_open_window'
                    }
                ]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwas100_table'),
                    url: g_ctxPath + 'crm/gwas100/getGwazlist',
                    exportsUrl: g_ctxPath + 'crm/gwas100/getGwazlist',
                    exportsName: '法人组织组织参数设置数据',
                    title: '法人组织参数设置信息表',
                    method: 'post',
                    id: 'gwas100_table',
                    size: _layuiConfig.ui_size,
                    autoSort: false, //禁用前端自动排序
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: '',
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-gwas100setting-action').innerHTML).render({}, function (string) {
                    gwas100SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwas100_table)', function (obj) {

                    layer.tips(gwas100SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        self._openTplFull({
                                            tag: 'gwas100_edit',
                                            tagData: obj,
                                            index: 0,
                                            openOpts: {
                                                title: '法人组织参数编辑',
                                                area: editArea
                                            }
                                        });
                                        break;
                                    case '1':
                                        self.delData(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行双击事件
                table.on('rowDouble(gwas100_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwas100_edit',
                        tagData: obj,
                        index: 0,
                        openOpts: {
                            title: '法人组织参数编辑',
                            area: editArea
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwas100_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwas100_edit',
                                tagData: {
                                    is_add: true
                                },
                                index: 0,
                                openOpts: {
                                    title: '法人组织参数编辑',
                                    area: editArea
                                }
                            });
                            break;
                        case 'del':
                            app.confirm({
                                msg: '是否删除选择的数据？',
                                ok: function () {
                                    self.delDataBatch();
                                }
                            });
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwas100_table', where: table_param});
        },

        delData: function (obj) {
            app.confirm({
                msg: '是否删除选择的信息？',
                ok: function () {
                    app.ajax({
                        xurl: 'crm/gwas100/del',
                        xdata: obj.data,
                        loading: true,
                        okmsg: '法人组织参数删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            });

        },

        delDataBatch: function () {
            var checkStatus = table.checkStatus('gwas100_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('crm_gwas100');
                    _temp.GWAZ001 = v.GWAZ001;
                    _temp.GWAZENT = v.GWAZENT;
                    _temp.GWAZLEGAL = v.GWAZLEGAL;
                    _xdata.push(_temp);
                });
                app.ajax({
                    xurl: 'crm/gwas100/delDataBatch',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    okmsg: '法人组织参数删除成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwas100',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_sysorg_biz',
                                        columnList: [
                                            {label: 'ID', prop: 'ID', width: '150'},
                                            {label: '归属法人', prop: 'NAME', width: '150'},
                                            {label: '营运中心编号', prop: 'BIZ_CODE', width: '150',sortable:false,unshow:true},
                                            // {label: '营运中心名称', prop: 'AZP02', width: '150',sortable:false,unshow:true}
                                        ],
                                    },
                                    cb: function (data) {
                                        vue.isearch.fields.GWAZLEGAL = data.ID;
                                    },
                                },
                                index: 1,
                                openOpts: {
                                    title: '选择归属法人',
                                    area: ['700px', '465px']
                                }
                            });
                        }
                    }
                }]
            });
            self.initTable();
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

    return new Gwas100();

});